# Maintainer: Guillaume ALAUX <guillaume@archlinux.org>

# TODO add test, see about packaging jtreg and using it here
# TODO see about building with OpenJDK9
# TODO add javazi
# TODO when IcedTea provides it, package icedtea-web and add it as optdepends
# TODO package OpenJFX for OpenJDK9 and add it as optdepends

## EXPLORATIONS
# TODO extract a jmods package from jdk9-openjdk?
# TODO extract a jdk-headless package?
#      Now that `jconsole` and `policytool` belong to JDK, one could extract
#      these to their own package.
#      FYI, `jdk8-openjdk` already depends on `jre8-openjdk` and not `jre8-openjdk-headless`

pkgname=('jre9-openjdk-headless' 'jre9-openjdk' 'jdk9-openjdk' 'openjdk9-src' 'openjdk9-doc')
_majorver=9
_updatever=181
pkgbase=java$_majorver-openjdk
pkgver=$_majorver.u$_updatever
_hg_tag="jdk-$_majorver+$_updatever"
_repo_ver=jdk$_java_ver.$_hg_tag
pkgrel=3
_pkg_full_ver=$pkgver-$pkgrel
arch=('i686' 'x86_64')
url='http://openjdk.java.net/'
license=('custom')
makedepends=('jdk8-openjdk' 'cpio' 'unzip' 'zip'
             'libelf' # required for AOT
             'libcups'
             'libx11' 'libxrender' 'libxtst' 'libxt' # 'libxext' already pulled by libxtst
             'alsa-lib'
             'pandoc' 'graphviz') # to generate full docs (as said in output of configure)
             # 'freetype2' already pulled
             # 'libjpeg-turbo' 'giflib' 'libpng' 'lcms2' already pulled
             # 'bash' OpenJDK scripts are explicitly Bash
checkdepends=()
_url_src=http://hg.openjdk.java.net/jdk9/jdk9
source=("jdk9u-${_repo_ver}.tar.gz::$_url_src/archive/$_hg_tag.tar.gz"
        "corba-${_repo_ver}.tar.gz::$_url_src/corba/archive/$_hg_tag.tar.gz"
        "hotspot-${_repo_ver}.tar.gz::$_url_src/hotspot/archive/$_hg_tag.tar.gz"
        "jaxp-${_repo_ver}.tar.gz::$_url_src/jaxp/archive/$_hg_tag.tar.gz"
        "jaxws-${_repo_ver}.tar.gz::$_url_src/jaxws/archive/$_hg_tag.tar.gz"
        "jdk-${_repo_ver}.tar.gz::$_url_src/jdk/archive/$_hg_tag.tar.gz"
        "langtools-${_repo_ver}.tar.gz::$_url_src/langtools/archive/$_hg_tag.tar.gz"
        "nashorn-${_repo_ver}.tar.gz::$_url_src/nashorn/archive/$_hg_tag.tar.gz"
        freedesktop-java.desktop
        freedesktop-jconsole.desktop
        freedesktop-policytool.desktop)
sha256sums=('02fd41467add2e02e0a4e278c0a2d99a8e2d986b640b66745e23d18dc9e7f230'
            '72c388d71e9610905a1b62e837fd8a277533766c1c47440c5af9c84f57337dbf'
            '1a0e239643c0157b940f3e4a3c545d99d317ba8b28dfdcf5ed2fd49853905602'
            'f963e67c791d96fc7e98126eff8fced4b6d457ea42174a4d51db74cc60cabdc9'
            '7071612b0c3a8e270e90999606fc60b9499a6cc87c872dd6f2e7e4b7e415e34b'
            '5c25d68928300df962ec5a339f8244542e701c79dd80fe04f72e0fa00d6ffc96'
            'f7120270546c7e26a58edfe811005f78f4f90cbf7c35dbad459a516395cc45f1'
            'd6b4f79182fda4abea7bb2c53181aa734a469772db1901c187dea8977885918a'
            '85d32321fb59a89e4f9ab320060597d4ca9d5e23b995c61549a48b41ac908a67'
            'bf197deb003879ed01adc6973a2430a41574f638667a5ecfbb1aad1403897ddc'
            '9394de47b5c38826ab837145816667d96b697998bafec5dd132d39d4846ac70e')

case "$CARCH" in
  x86_64) _JARCH='x86_64';;
  i686)   _JARCH='x86';;
esac

_jvmdir=/usr/lib/jvm/java-$_majorver-openjdk
_imgdir=jdk$_majorver-$_hg_tag/build/linux-$_JARCH-normal-server-release/images

_nonheadless=(lib/libawt_xawt.{so,diz}
              lib/libjawt.{so,diz}
              lib/libjsoundalsa.{so,diz}
              lib/libsplashscreen.{so,diz})

prepare() {
  cd "jdk$_majorver-$_hg_tag"
  for subrepo in corba hotspot jdk jaxws jaxp langtools nashorn
  do
    ln -s ../$subrepo-$_hg_tag $subrepo
  done
}

build() {
  cd "jdk$_majorver-$_hg_tag"

  NUM_PROC_OPT=''
  MAKEFLAG_J=$(echo $MAKEFLAGS | sed -En 's/.*-j([0-9]+).*/\1/p')
  if [ -n $MAKEFLAG_J ]; then
    # http://hg.openjdk.java.net/jdk9/jdk9/file/85e6cb013b98/make/InitSupport.gmk#l105
    echo "Removing '-j$MAKEFLAG_J' from MAKEFLAGS to prevent build fail. Passing it directly to ./configure."
    export MAKEFLAGS=${MAKEFLAGS/-j$MAKEFLAG_J/}
    NUM_PROC_OPT="--with-num-cores=$MAKEFLAG_J"
  fi

  _CFLAGS=$CFLAGS
  _CXXFLAGS=$CXXFLAGS
  _LDFLAGS=$LDFLAGS
  if [[ $CARCH = i686 ]]; then
    echo "Removing '-fno-plt' from CFLAGS and CXXFLAGS to prevent build fail with this architecture"
    _CFLAGS=${CFLAGS/-fno-plt/}
    _CXXFLAGS=${CXXFLAGS/-fno-plt/}
  fi

  # CFLAGS, CXXFLAGS and LDFLAGS are ignored as shown by a warning
  # in the output of ./configure unless used like such:
  #  --with-extra-cflags="$CFLAGS"
  #  --with-extra-cxxflags="$CXXFLAGS"
  #  --with-extra-ldflags="$LDFLAGS"
  # See also paragraph "Configure Control Variables from "jdk$_majorver-$_hg_tag/common/doc/building.md
  unset CFLAGS
  unset CXXFLAGS
  unset LDFLAGS

  bash configure \
    --with-version-build="$_updatever" \
    --with-version-pre="" \
    --with-version-opt="" \
    --with-stdc++lib=dynamic \
    --with-extra-cflags="$_CFLAGS" \
    --with-extra-cxxflags="$_CXXFLAGS" \
    --with-extra-ldflags="$_LDFLAGS" \
    --with-libjpeg=system \
    --with-giflib=system \
    --with-libpng=system \
    --with-lcms=system \
    --with-zlib=system \
    --enable-unlimited-crypto \
    --disable-warnings-as-errors \
    $NUM_PROC_OPT
    #--disable-javac-server

  make images docs
}

# check() {
#   cd "$pkgname-$pkgver"
#   make -k check
# }

package_jre9-openjdk-headless() {
  pkgdesc="OpenJDK Java $_majorver headless runtime environment"
  depends=('java-runtime-common>=3' 'ca-certificates-utils' 'nss')
  optdepends=('java-rhino: for some JavaScript support')
  provides=("java-runtime-headless=$_majorver" "java-runtime-headless-openjdk=$_majorver")
  backup=(etc/$pkgbase/logging.properties
          etc/$pkgbase/management/jmxremote.access
          etc/$pkgbase/management/jmxremote.password.template
          etc/$pkgbase/management/management.properties
          etc/$pkgbase/net.properties
          etc/$pkgbase/security/java.policy
          etc/$pkgbase/security/java.security
          etc/$pkgbase/security/policy/README.txt
          etc/$pkgbase/security/policy/limited/default_US_export.policy
          etc/$pkgbase/security/policy/limited/default_local.policy
          etc/$pkgbase/security/policy/limited/exempt_local.policy
          etc/$pkgbase/security/policy/unlimited/default_US_export.policy
          etc/$pkgbase/security/policy/unlimited/default_local.policy
          etc/$pkgbase/sound.properties)
  install=install_jre9-openjdk-headless.sh

  cd $_imgdir/jre

  install -d -m 755 "$pkgdir$_jvmdir"

  cp -a bin lib \
    "$pkgdir$_jvmdir"

  for f in ${_nonheadless[@]}; do
    rm "$pkgdir$_jvmdir/$f"
  done

  cp ../jdk/release "$pkgdir$_jvmdir"
  cp ../jdk/lib/modules "$pkgdir$_jvmdir/lib"

  # Conf
  install -d -m 755 "$pkgdir/etc"
  cp -r conf "$pkgdir/etc/$pkgbase"
  ln -s /etc/$pkgbase "$pkgdir/$_jvmdir/conf"

  # Legal
  install -d -m 755 "$pkgdir/usr/share/licenses"
  cp -r legal "$pkgdir/usr/share/licenses/$pkgbase"
  ln -s $pkgbase "$pkgdir/usr/share/licenses/$pkgname"
  ln -s /usr/share/licenses/$pkgbase "$pkgdir/$_jvmdir/legal"

  # Man pages
  for f in man/man1/* man/ja/man1/*; do
    install -D -m 644 $f "$pkgdir/usr/share/${f/\.1/-openjdk9.1}"
  done
  ln -s /usr/share/man "$pkgdir/$_jvmdir/man"

  # Link JKS keystore from ca-certificates-utils
  rm -f "$pkgdir$_jvmdir/lib/security/cacerts"
  ln -sf /etc/ssl/certs/java/cacerts "$pkgdir$_jvmdir/lib/security/cacerts"
}

package_jre9-openjdk() {
  pkgdesc="OpenJDK Java $_majorver full runtime environment"
  depends=("jre$_majorver-openjdk-headless=$_pkg_full_ver")
  optdepends=('alsa-lib: for basic sound support'
              'gtk2: for the Gtk+ 2 look and feel - desktop usage'
              'gtk3: for the Gtk+ 3 look and feel - desktop usage')
  provides=("java-runtime=$_majorver" "java-runtime-openjdk=$_majorver")
  install=install_jre9-openjdk.sh

  cd $_imgdir/jre

  install -d -m 755 "$pkgdir$_jvmdir"

  for f in ${_nonheadless[@]}; do
    install -D -m 644 $f "$pkgdir$_jvmdir/$f"
  done

  # Licenses
  install -d -m 755 "$pkgdir/usr/share/licenses"
  ln -s $pkgbase "$pkgdir/usr/share/licenses/$pkgname"
}

package_jdk9-openjdk() {
  pkgdesc="OpenJDK Java $_majorver development kit"
  depends=("jre$_majorver-openjdk=$_pkg_full_ver" 'java-environment-common=3' 'hicolor-icon-theme' 'libelf')
  provides=("java-environment=$_majorver" "java-environment-openjdk=$_majorver")
  install=install_jdk9-openjdk.sh

  cd $_imgdir/jdk

  install -d -m 755 "$pkgdir$_jvmdir"

  cp -a bin demo include jmods lib \
    "$pkgdir$_jvmdir"

  rm "$pkgdir$_jvmdir/lib/src.zip"

  # Remove files held by JRE
  pushd ../jre
  for d in bin lib; do
    find $d ! -type d -exec rm "$pkgdir$_jvmdir/{}" \;
  done
  popd
  find "$pkgdir$_jvmdir/lib" -type d -empty -delete

  # Conf files all belong to JRE

  # Legal
  install -d -m 755 "$pkgdir/usr/share/licenses"
  cp -r legal "$pkgdir/usr/share/licenses/$pkgbase"
  pushd ../jre/legal
  find . ! -type d -exec rm "$pkgdir/usr/share/licenses/$pkgbase/{}" \;
  popd
  find "$pkgdir/usr/share/licenses" -type d -empty -delete
  ln -s $pkgbase "$pkgdir/usr/share/licenses/$pkgname"

  # Man pages
  for f in man/man1/* man/ja/man1/*; do
    if [ ! -e ../jre/$f ]; then
      install -D -m 644 $f "$pkgdir/usr/share/${f/\.1/-openjdk$_majorver.1}"
    fi
  done

  # Icons
  for s in 16 24 32 48; do
    install -D -m 644 \
      $srcdir/jdk-$_hg_tag/src/java.desktop/unix/classes/sun/awt/X11/java-icon$s.png \
      "$pkgdir/usr/share/icons/hicolor/$sx$s/apps/$pkgbase.png"
  done

  # Desktop files
  for f in jconsole policytool java; do
    install -D -m 644 \
      "$srcdir/freedesktop-$f.desktop" \
      "$pkgdir/usr/share/applications/$f-$pkgbase.desktop"
  done
}

package_openjdk9-src() {
  pkgdesc="OpenJDK Java $_majorver sources"
  # Depends on JDK to get license files
  depends=("jdk$_majorver-openjdk=$_pkg_full_ver")

  install -D -m 644 -t "$pkgdir$_jvmdir/lib" $_imgdir/jdk/lib/src.zip

  install -d -m 755 "$pkgdir/usr/share/licenses"
  ln -s $pkgbase "$pkgdir/usr/share/licenses/$pkgname"
}

package_openjdk9-doc() {
  pkgdesc="OpenJDK Java $_majorver documentation"
  # Depends on JDK to get license files
  depends=("jdk$_majorver-openjdk=$_pkg_full_ver")

  install -d -m 755 "$pkgdir/usr/share/doc"
  cp -r $_imgdir/docs "$pkgdir/usr/share/doc/$pkgbase"

  install -d -m 755 "$pkgdir/usr/share/licenses"
  ln -s $pkgbase "$pkgdir/usr/share/licenses/$pkgname"
}
